package handler;

import entity.CourseDO;
import service.CourseService;
import service.impl.CourseServiceImpI;
import view.AddCourseView;
import view.CourseView;
import view.MainView;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class AddCourseViewHandler implements ActionListener {
    private AddCourseView addCourseView;
    private CourseView courseView;

    public AddCourseViewHandler(AddCourseView addCourseView, CourseView courseView) {
        this.addCourseView = addCourseView;
        this.courseView = courseView;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JButton jButton = (JButton) e.getSource();
        String text = jButton.getText();
        if ("添加".equals(text)) {
            CourseService courseService = new CourseServiceImpI();
            CourseDO courseDO = addCourseView.buildCourseDO();
            boolean addResult = courseService.add(courseDO);
            if (addResult) {
                //重新加载表格
                courseView.reloadTable();
                addCourseView.dispose();
            } else {
                JOptionPane.showMessageDialog(addCourseView, "添加失败");
            }
        }
    }
}
